package com.shaoshaohuo.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2017/7/6.
 */

public class Vpindicator extends LinearLayout implements ViewPager.OnPageChangeListener {


    public Vpindicator(@NonNull Context context) {
        this(context, null);


    }

    public Vpindicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Vpindicator(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
    }

    private

    Bitmap imgdownid;
    private

    Bitmap imgallwnid;
   private int size;
    public void setnum(Bitmap imgdownid, Bitmap imgallwnid, int num, int margin) {
        this.imgdownid = imgdownid;
        this.imgallwnid = imgallwnid;
        this.size=num;
        removeAllViews();
        for (int i = 0; i < num; i++) {
            ImageView imageView = new ImageView(getContext());
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(margin, 0, 0, 0);

            imageView.setLayoutParams(layoutParams);
            addView(imageView);
            if (i == 0) {
                imageView.setImageBitmap(imgdownid);
            } else {
                imageView.setImageBitmap(imgallwnid);
            }
        }
    }

    private ViewPager viewPager;

    public void setvp(ViewPager viewPager) {
        this.viewPager = viewPager;
        viewPager.addOnPageChangeListener(this);
    }

    public void setindex(int postion) {
        for (int i = 0; i < getChildCount(); i++) {
            ((ImageView) getChildAt(i)).setImageBitmap(imgallwnid);
        }
        if( ((ImageView) getChildAt(postion))!=null){
            ((ImageView) getChildAt(postion)).setImageBitmap(imgdownid);
        }

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

        int a = i % size;
        Log.e("TAG", "onPageSelected: " + a);
        setindex(a);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
